//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartFormulaView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartFormulaView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CBCGPChartData CALLBACK TransitionCallbackFunc(const CArray<CBCGPChartData, CBCGPChartData>& arData,
					int /*nDataPointIndex*/, CBCGPChartSeries* /*pFormulaSeries*/, LPARAM lParam)
{
	int nFormula = (int)lParam;

	double A = arData[0].GetValue();
	double B = arData[1].GetValue();

	double dblRes = 0;

	switch (nFormula)
	{
	case 0:
		dblRes = A * A + B * B;
		break;

	case 1:
		dblRes = log(A * B);
		break;

	case 2:
		dblRes = (exp(A) / exp(B)) / 2;
		break;

	case 3:
		dblRes = sqrt(A * A + B);
		break;
	}

	return CBCGPChartData(dblRes);
}

/////////////////////////////////////////////////////////////////////////////
// CChartFormulaView

IMPLEMENT_DYNCREATE(CChartFormulaView, CBCGPChartExampleView)

CChartFormulaView::CChartFormulaView()
	: CBCGPChartExampleView(CChartFormulaView::IDD)
{
	//{{AFX_DATA_INIT(CChartFormulaView)
	m_nIsCustomFormula = 0;
	m_nFormulaType = 0;
	m_nCallBackType = 0;
	//}}AFX_DATA_INIT
}

CChartFormulaView::~CChartFormulaView()
{
}

void CChartFormulaView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartFormulaView)
	DDX_Control(pDX, IDC_CALLBACK_TYPE, m_wndCallbackType);
	DDX_Control(pDX, IDC_FORMULA_TYPE, m_wndFormulaType);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Radio(pDX, IDC_FORMULA_1, m_nIsCustomFormula);
	DDX_CBIndex(pDX, IDC_FORMULA_TYPE, m_nFormulaType);
	DDX_CBIndex(pDX, IDC_CALLBACK_TYPE, m_nCallBackType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartFormulaView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartFormulaView)
	ON_BN_CLICKED(IDC_FORMULA_1, OnUpdateChart)
	ON_BN_CLICKED(IDC_FORMULA_2, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_FORMULA_TYPE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_CALLBACK_TYPE, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartFormulaView diagnostics

#ifdef _DEBUG
void CChartFormulaView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartFormulaView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartFormulaView message handlers

void CChartFormulaView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->ShowDataMarkers();
	pChart->ShowDataLabels();

	CBCGPChartSeries* pSeriesA = pChart->CreateSeries(_T("Series A"));
	pSeriesA->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE);

	CBCGPChartSeries* pSeriesB = pChart->CreateSeries(_T("Series B"));
	pSeriesB->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE_HERMITE);

	pSeriesA->AddDataPoint(1.);
	pSeriesA->AddDataPoint(2.);
	pSeriesA->AddDataPoint(2.5);
	pSeriesA->AddDataPoint(3.);
	pSeriesA->AddDataPoint(3.5);
	pSeriesA->AddDataPoint(5.);
	pSeriesA->AddDataPoint(6.);
	pSeriesA->AddDataPoint(4.);

	pSeriesB->AddDataPoint(6.);
	pSeriesB->AddDataPoint(4.5);
	pSeriesB->AddDataPoint(4.);
	pSeriesB->AddDataPoint(4.);
	pSeriesB->AddDataPoint(3.5);
	pSeriesB->AddDataPoint(3.0);
	pSeriesB->AddDataPoint(2.5);
	pSeriesB->AddDataPoint(1.);

	CBCGPChartSeries* pSeriesResult = pChart->CreateSeries(_T("Result"));
	pSeriesResult->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE_HERMITE);

	pSeriesResult->SetDataLabelDataFormat(_T("%.2G"));

	CBCGPChartTransitionFormula formula(CBCGPChartTransitionFormula::TT_SUM);
	
	formula.AddInputSeries(pSeriesA);
	formula.AddInputSeries(pSeriesB);

	pSeriesResult->SetFormula(formula);

	SetDefaultLineWidth();
	OnUpdateChart();
}

void CChartFormulaView::OnUpdateChart() 
{
	UpdateData();

	m_wndFormulaType.EnableWindow(m_nIsCustomFormula == 0);
	m_wndCallbackType.EnableWindow(m_nIsCustomFormula == 1);
	
	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPChartSeries* pSeriesResult = pChart->GetSeries(2);
	ASSERT_VALID(pSeriesResult);

	CBCGPChartTransitionFormula* pFormula = DYNAMIC_DOWNCAST(CBCGPChartTransitionFormula, pSeriesResult->GetFormula());
	ASSERT_VALID(pFormula);

	if (m_nIsCustomFormula == 0)
	{
		pFormula->SetTransitionCallback(NULL);
		pFormula->SetTransitionType((CBCGPChartTransitionFormula::TransitionType)m_nFormulaType);
	}
	else
	{
		pFormula->SetTransitionCallback(TransitionCallbackFunc, m_nCallBackType);
	}

	pChart->SetDirty(TRUE, TRUE);
}
